package cn.xiaoliu.org.controller;


import cn.xiaoliu.basic.utils.AjaxResult;
import cn.xiaoliu.basic.utils.PageList;
import cn.xiaoliu.org.domain.Employee;
import cn.xiaoliu.org.query.EmployeeQuery;
import cn.xiaoliu.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    //查询一个  get
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    //查询全部  get
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    //删除  delete
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }
    //批量删除
    @PatchMapping
    public AjaxResult pachRemove(@RequestBody List<Long> ids){
        try {
            employeeService.pachRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }

    //修改新增  put
    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }
    //分页加高级查询  post
    @PostMapping("/list")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }
}
